﻿// MainForm.cs
using System;
using System.Data.SQLite;
using System.Windows.Forms;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace YouGeBookmark
{
    public partial class MainForm : Form
    {
        private readonly string _url;

        public MainForm(string url)
        {
            InitializeComponent();
            _url = url;
            InitializeDatabase();
        }

        private void InitializeDatabase()
        {
            using var conn = new SQLiteConnection("Data Source=bookmarks.db;Version=3;");
            conn.Open();

            string sql = @"CREATE TABLE IF NOT EXISTS Bookmarks (
                            ID INTEGER PRIMARY KEY AUTOINCREMENT,
                            URL TEXT NOT NULL,
                            Tags TEXT,
                            Notes TEXT,
                            CreateDate DATETIME DEFAULT CURRENT_TIMESTAMP
                          );";
            new SQLiteCommand(sql, conn).ExecuteNonQuery();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using var conn = new SQLiteConnection("Data Source=bookmarks.db;Version=3;");
            conn.Open();

            string sql = @"INSERT INTO Bookmarks (URL, Tags, Notes) 
                          VALUES (@url, @tags, @notes)";
            var cmd = new SQLiteCommand(sql, conn);
            cmd.Parameters.AddWithValue("@url", _url);
            cmd.Parameters.AddWithValue("@tags", string.Join(",", cblTags.CheckedItems.Cast<string>().ToList()));
            cmd.Parameters.AddWithValue("@notes", txtNotes.Text);
            cmd.ExecuteNonQuery();

            MessageBox.Show("保存成功！");
            this.Close();
        }

        private void btnManageTags_Click(object sender, EventArgs e)
        {
            var tagForm = new TagManagerForm();
            tagForm.ShowDialog();
            LoadTags();
        }

        private void LoadTags()
        {
            cblTags.Items.Clear();
            cblTags.Items.AddRange(new object[] { "时政", "财经", "军事", "科技", "行业" });
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadTags();
            this.Text += $" - {_url}";
        }

        // Windows 窗体设计器生成的代码
        private System.ComponentModel.IContainer components = null;
        private CheckedListBox cblTags;
        private TextBox txtNotes;
        private Button btnSave;
        private Button btnManageTags;
        private System.Windows.Forms.Label lblTags;
        private Button btnQuery;
        private System.Windows.Forms.Label lblNotes;
        private void InitializeComponent()
        {
            cblTags = new CheckedListBox();
            txtNotes = new TextBox();
            btnSave = new Button();
            btnManageTags = new Button();
            lblTags = new Label();
            lblNotes = new Label();
            btnQuery = new Button();
            SuspendLayout();
            // 
            // cblTags
            // 
            cblTags.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
            cblTags.FormattingEnabled = true;
            cblTags.Location = new System.Drawing.Point(12, 32);
            cblTags.Name = "cblTags";
            cblTags.Size = new System.Drawing.Size(180, 238);
            cblTags.TabIndex = 0;
            // 
            // txtNotes
            // 
            txtNotes.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            txtNotes.Location = new System.Drawing.Point(198, 32);
            txtNotes.Multiline = true;
            txtNotes.Name = "txtNotes";
            txtNotes.ScrollBars = ScrollBars.Vertical;
            txtNotes.Size = new System.Drawing.Size(294, 244);
            txtNotes.TabIndex = 1;
            // 
            // btnSave
            // 
            btnSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnSave.Location = new System.Drawing.Point(336, 282);
            btnSave.Name = "btnSave";
            btnSave.Size = new System.Drawing.Size(75, 30);
            btnSave.TabIndex = 2;
            btnSave.Text = "保存";
            btnSave.UseVisualStyleBackColor = true;
            btnSave.Click += btnSave_Click;
            // 
            // btnManageTags
            // 
            btnManageTags.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            btnManageTags.Location = new System.Drawing.Point(12, 282);
            btnManageTags.Name = "btnManageTags";
            btnManageTags.Size = new System.Drawing.Size(100, 30);
            btnManageTags.TabIndex = 3;
            btnManageTags.Text = "管理标签";
            btnManageTags.UseVisualStyleBackColor = true;
            btnManageTags.Click += btnManageTags_Click;
            // 
            // lblTags
            // 
            lblTags.AutoSize = true;
            lblTags.Location = new System.Drawing.Point(12, 9);
            lblTags.Name = "lblTags";
            lblTags.Size = new System.Drawing.Size(68, 17);
            lblTags.TabIndex = 4;
            lblTags.Text = "选择标签：";
            // 
            // lblNotes
            // 
            lblNotes.AutoSize = true;
            lblNotes.Location = new System.Drawing.Point(198, 9);
            lblNotes.Name = "lblNotes";
            lblNotes.Size = new System.Drawing.Size(68, 17);
            lblNotes.TabIndex = 5;
            lblNotes.Text = "备注说明：";
            // 
            // btnQuery
            // 
            btnQuery.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnQuery.Location = new System.Drawing.Point(417, 282);
            btnQuery.Name = "btnQuery";
            btnQuery.Size = new System.Drawing.Size(75, 30);
            btnQuery.TabIndex = 6;
            btnQuery.Text = "查询";
            btnQuery.UseVisualStyleBackColor = true;
            btnQuery.Click += btnQuery_Click;
            // 
            // MainForm
            // 
            AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new System.Drawing.Size(504, 324);
            Controls.Add(btnQuery);
            Controls.Add(lblNotes);
            Controls.Add(lblTags);
            Controls.Add(btnManageTags);
            Controls.Add(btnSave);
            Controls.Add(txtNotes);
            Controls.Add(cblTags);
            MinimumSize = new System.Drawing.Size(520, 360);
            Name = "MainForm";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "优哥书签管理";
            Load += MainForm_Load;
            ResumeLayout(false);
            PerformLayout();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            //var tagForm = new TagManagerForm();
            //tagForm.ShowDialog();
            var qf = new QueryForm();
            qf.ShowDialog();
        }
    }
}